﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace QExcel
{
    public class QColumns : QColumn
    {
        public QSheet Sheet
        {
            get;
            set;
        }

        public QTable Table
        {
            get;
            set;
        }

        public int Count
        {
            get
            {
                count = 0;
                this.GetColumnsCount(this.columns);
                return count;
            }
        }

        int count = 0;
        private void GetColumnsCount(List<QColumn> columns)
        {
            foreach (QColumn clumn in columns)
            {
                if (clumn.LeafColumns.Count == 0)
                {
                    count++;
                }
                else
                {
                    this.GetColumnsCount(clumn.LeafColumns);
                }
            }
        }


        public QColumn GetColumnByName(string name)
        {
            foreach (QColumn cl in this.columns)
            {
                if (cl.Name == name)
                {
                    return cl;
                }
            }

            return null;
        }

        List<QColumn> leafColumns = new List<QColumn>();
        /// <summary>
        /// 获取最底层的叶子节点
        /// </summary>
        public List<QColumn> AllLeafColumns
        {
            get
            {
                leafColumns.Clear();
                this.GetLeafColumns(this.columns);
                return leafColumns;
            }
        }

        private void GetLeafColumns(List<QColumn> list)
        {
            foreach (QColumn column in list)
            {
                if (column.LeafColumns.Count == 0)
                {
                    leafColumns.Add(column);
                }
                else
                {
                    this.GetLeafColumns(column.LeafColumns);
                }
            }
        }

        private int depth = 1;
        public int Depth
        {
            get
            {
                return this.GetColumnsDepth();
            }
        }

        public override void AddColumn(QColumn clumn)
        {
            clumn.Table = this.Table;
            clumn.SetCurIndex(this.columns.Count + 1);
            this.columns.Add(clumn);
        }

        private int GetColumnsDepth()
        {
            int depth = 0;
            foreach (QColumn clumn in columns)
            {
                if (depth < clumn.Depth)
                {
                    depth = clumn.Depth;
                }
            }
            return depth;
        }

        private List<QColumn> columns = new List<QColumn>();
        public List<QColumn> Columns
        {
            get
            {
                return columns;
            }
            set
            {
                columns = value;
            }
        }
    }
}
